const app = getApp()
import BaseModel from '../../utils/common.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:"",//客户详情点击共享传过来用户id
    shareId:"",//添加用户默认传过来一个id
    color: '#bbb', //选择按钮颜色
    page:1,
    size:999, //每页条数
    listData:[],//获取到的数据
    countAll:"",//所有员工数量
    staffIdAll:[], //选中的员工id存在数组里
    is_clickTab:0,
    code:'',
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getStaffAllData();
    this.getUserAlreadyData();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      code:options.code,
    })
  },
  /*全部员工数据*/
  getStaffAllDataModel: new BaseModel({
    url: 'getStaff',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getStaffAllData() {
    this.getStaffAllDataModel
        .fetch({
          "type":1,
          "user_id":this.data.id,
          "page": this.data.page,
          "size": this.data.size
        })
        .then(res => {
          if(this.data.is_clickTab==0){
            this.setData({
              listData:res.data,
              countAll:res.count
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              //成功
              var listData_list = this.data.listData;
              for(var i=0;i<res.data.length;i++){
                listData_list.push(res.data[i])
              }
              this.setData({
                listData: listData_list,
                countAll:res.count
              })
            }
          }
        })
        .catch(res => {
          //失败
          this.setData({
            footer_more:"没有更多数据了"
          })
        })
  },
  /*复选框选中事件*/
  checkboxChange: function(e) {
    this.setData({
      staffIdAll:e.detail.value.toString()
    })
    console.log(e.detail.value.toString())
  },
  /*点击确定操作*/
  click_staff:function(){
    var that = this;
    let staffIdAll = that.data.staffIdAll;
    // 最后赋值到data中渲染到页面
    this.setData({
      staffIdAll: staffIdAll
    });
    this.getCustomerSeleteData();
  },
  //获取已获取的vip
  getUserAlreadyModel: new BaseModel({
    url: "employees/shareUserId",
    needAjaxLoading: true,
    method: "get",
    loadingText: "玩命加载中"
  }),
  getUserAlreadyData() {
    this.getUserAlreadyModel
        .fetch({
          user_id:this.data.id
        })
        .then(res => {
          //成功
          this.setData({
            staffIdAll:res.data
           });
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /*提交分享数据*/
  getCustomerSeleteDataModel:new BaseModel({
    url: "shareUser",
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getCustomerSeleteData(){
    var that = this;
    this.getCustomerSeleteDataModel
        .fetch({
          id:that.data.id,  //分享的客户id
          staffIdAll:that.data.staffIdAll,  //接收分享的员工
          code:that.data.code
        })
        .then(res => {
          //成功
          wx.navigateTo({
            url:"../staff_management/staff_shareSuccess"
          })
        })
        .catch(res => {
          //失败
        })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getStaffAllData();
    wx.stopPullDownRefresh(); //停止下拉
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.data.page+=1;
    this.setData({
      is_clickTab:1
    })
    this.getStaffAllData();
  },

  /**
   * 用户点击右上角分享
   */
  /* onShareAppMessage: function () {

   }*/
})